//WEBSOCKET INIT AND HANDLERS
var webSocket;
var nickname;

function onMessage(msg) {
    
    var target = document.getElementById("target");
    url = window.webkitURL.createObjectURL(msg.data);
    target.onload = function() {
          window.webkitURL.revokeObjectURL(url);
    };
    target.src = url;
}

function onOpen() {
}

function onError(event) {
    alert(event.data);
}

function onClose(event){
}

function start() {
    return false;
}

function sendMessage() {
    
    webSocket.send("yoho...");
}

function privateMessagePopup(message, sender){
    var description = "Private message from " + sender 
    + ": " + message;
    var resp = window.prompt(description,"Write a response..");
    
    if(resp != 'Write a response..')
        sendPrivateMessage(resp, sender);
}

function sendPrivateMessage(msg, receiver){
    var pMsg = {
        type: "private",
        message: msg,
        receiver: receiver,
        sender: nickname
    };
    webSocket.send(JSON.stringify(pMsg));
}

function setUserslist(userlist){
    console.log("Userlist var:")
    console.log(userlist);
    
    var table = "<table border ='0'>";
    var tr = "<tr>";
    var _tr ="</tr>";
    var td = "<td>";
    var _td = "</td>";
    var _table ="</table>";
       
    var userslistHtml = table;   
    for (var i=0; i<userlist.length; i++){
        userslistHtml += tr+td + "<button style='background:none;" + 
        "border:none; margin:0; padding:0;' onclick='newPrivateMessage(&quot;" +
        userlist[i] + "&quot;)' >" + userlist[i] + "</button>" + _td + _tr;
    }
    userslistHtml += _table;
    document.getElementById("usersList").innerHTML = userslistHtml;   
}

function newPrivateMessage(username){
    var message = window.prompt("Send a private message to " + username+"." ,"Message..");
    sendPrivateMessage(message,username);
}

function connectBtn(){
    if (webSocket.readyState == 1){ // 1 is connected
        console.log("websocket.readyState is 1");
    }else{
        console.log("websocket.readyState is 0");
    }
}

function disconnect(){
    webSocket.close();
    document.getElementById("statusLine").innerHTML = "Disconnected";
}

function changeNickname(){
    setNicknameVariable();
    sendNicknameMessage();
}

function sendNicknameMessage(){
    var nicknameMessage = {
        type: "nickname",
        nickname: nickname
    };
    webSocket.send(JSON.stringify(nicknameMessage));
}

function closingCode(){
   window.onbeforeunload = null;
   websocket.close();
}

function onLoad(){
    window.onbeforeunload = closingCode;
    console.log("Onload has been ran");
    connect(); 
}

function connect(){
    webSocket = new WebSocket('ws://127.0.0.1:8080/webcamApp/cam');

    webSocket.onerror = function(event) {
        onError(event);
    };

    webSocket.onopen = function(event) {
        onOpen(event);
    };

    webSocket.onmessage = function(event) {
        onMessage(event);
    };

    webSocket.onclose = function(event){
        onClose(event);
    };
}